package com.intct.hbase.dml;

import com.intct.hbase.util.HBaseConnection;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-07-11 10:02
 */
public class DeleteData {
    public static void main(String[] args) throws IOException {
        // 连接HBase
        Connection connection = HBaseConnection.getConnection();

        // 声明一个TableName实例
        TableName hbaseTableName = TableName.valueOf("intct82", "t001");

        // 创建Admin实例
        Admin admin = connection.getAdmin();

        // 判断存在，则删除
        if (admin.tableExists(hbaseTableName)) {
            // 创建Table实例
            Table table = connection.getTable(hbaseTableName);

            // 删除数据
            Delete delete = new Delete(Bytes.toBytes("1001"));

            // 删除之后，再查询会显示上一版本数据
            // delete.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));

            // 删除之后，把所有版本数据删除
            delete.addColumns(Bytes.toBytes("f1"), Bytes.toBytes("age"));

            try {
                table.delete(delete);
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 关闭资源
            if (table != null) {
                table.close();
            }
        }


        if (admin != null) {
            admin.close();
        }
    }
}
